package drds.data_propagate.binlog_event.secondary.position;

import lombok.Getter;
import lombok.Setter;

/**
 * 数据库对象的唯一标示
 */
public class EntryPosition extends TimePosition {

    private static final long serialVersionUID = 81432665066427482L;

    @Setter
    @Getter
    private String binlogFileName;
    @Setter
    @Getter
    private Long binlogFileOffset;
    //
    @Setter
    @Getter
    private Long serverId = null; // 记录一下位点对应的serverId
    @Setter
    @Getter
    private String gtid = null;
    @Setter
    @Getter
    private boolean included = false;

    public EntryPosition() {
        super(null);
    }

    public EntryPosition(Long timestamp) {
        this(null, null, timestamp);
    }

    public EntryPosition(String binlogFileName, Long binlogFileOffset) {
        this(binlogFileName, binlogFileOffset, null);
    }

    public EntryPosition(String binlogFileName, Long binlogFileOffset, Long timestamp) {
        super(timestamp);
        this.binlogFileName = binlogFileName;
        this.binlogFileOffset = binlogFileOffset;
    }

    public EntryPosition(Long serverId, String binlogFileName, Long binlogFileOffset, Long timestamp) {
        this(binlogFileName, binlogFileOffset, timestamp);
        this.serverId = serverId;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((binlogFileName == null) ? 0 : binlogFileName.hashCode());
        result = prime * result + ((binlogFileOffset == null) ? 0 : binlogFileOffset.hashCode());
        // 手写equals，自动生成时需注意
        result = prime * result + ((executeTimestamp == null) ? 0 : executeTimestamp.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (!super.equals(object)) {
            return false;
        }
        if (!(object instanceof EntryPosition)) {
            return false;
        }
        EntryPosition other = (EntryPosition) object;
        if (binlogFileName == null) {
            if (other.binlogFileName != null) {
                return false;
            }
        } else if (!binlogFileName.equals(other.binlogFileName)) {
            return false;
        }
        if (binlogFileOffset == null) {
            if (other.binlogFileOffset != null) {
                return false;
            }
        } else if (!binlogFileOffset.equals(other.binlogFileOffset)) {
            return false;
        }
        // 手写equals，自动生成时需注意
        if (executeTimestamp == null) {
            if (other.executeTimestamp != null) {
                return false;
            }
        } else if (!executeTimestamp.equals(other.executeTimestamp)) {
            return false;
        }
        return true;
    }


    public int compareTo(EntryPosition entryId) {
        final int compareTo = binlogFileName.compareTo(entryId.binlogFileName);

        if (compareTo == 0) {
            return (int) (binlogFileOffset - entryId.binlogFileOffset);
        }
        return compareTo;
    }

}
